package com.ruoyi.auth;
import com.ruoyi.auth.Jurisdiction;
import com.ruoyi.common.core.constant.TokenConstants;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.exception.InnerAuthException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Aspect
@Component
public class JurisdictionAsperct {
//    @Autowired
//    private AuditMapper auditMapper;

    @Autowired
    private HttpServletRequest request;


    @Around("@annotation(Jurisdiction)")
    public Object jurisdictionAsperct(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        boolean b = this.jurisdictionAnnotation(signature.getMethod(), request);
        if (b){
            return point.proceed();
        }
        return null;
    }

    public boolean jurisdictionAnnotation(Method method,HttpServletRequest request){
        Jurisdiction annotation = method.getAnnotation(Jurisdiction.class);
        if (annotation!=null){
            SecurityContextHolder.setPermission(StringUtils.join(annotation.value(), ""));
            boolean b = this.jurisSql(annotation.value(), request);
            return b;
        }
        return true;
    }
    public boolean jurisSql(String sValues,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token==null){
            return false;
        }
        Claims chengyibo = Jwts.parser().setSigningKey("abcdefghijklmnopqrstuvwxyz").parseClaimsJws(token).getBody();
        Object uid = chengyibo.get("uid");
        String id = chengyibo.getId();
//
//        Jurisdict jurisdict = new Jurisdict();
//        jurisdict.setId(uid+"");
//        jurisdict.setJurisdictionname(sValues);
//        Jurisdict js = auditMapper.js(jurisdict);
//
//        if (js==null || js.getCo()<=0){
//            return false;
//        }
        return true;
    }


}
